package com.ckh.springboot.controller;


import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckh.springboot.common.Result;
import com.ckh.springboot.entity.*;
import com.ckh.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ckh.springboot.service.IGoodsService;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ckh
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {


    @Value("${files.upload.path}")
    private String fileUploadPath;

    @Resource
    private IGoodsService goodsService;

    @Resource
    private IUserService userService;

    // 新增或者更新
    @PostMapping
    public Result save(@RequestBody Goods goods) {
        return Result.success(goodsService.saveOrUpdate(goods));
    }


    // 新增或者更新
    @PostMapping("/personGoods/{goodsId}/{personId}")
    public Result personGoods(@PathVariable Integer goodsId, @PathVariable Integer personId) {
        goodsService.setPersonGoods(goodsId,personId);
        return Result.success();
    }

    @PostMapping("/deletePersonGoods/{goodsId}/{personId}")
    public Result deletePersonGoods(@PathVariable Integer goodsId, @PathVariable Integer personId) {
        goodsService.deletePersonGoods(goodsId,personId);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(goodsService.removeById(id));
    }


    @GetMapping
    public Result findAll() {
        return Result.success(goodsService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(goodsService.getById(id));
    }


    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
//        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
//        queryWrapper.like("name",name);
//        queryWrapper.orderByDesc("id");
//
//        List<Goods> records = page.getRecords();
//        for(Goods record : records){
//            User user = userService.getById(record.getStoreId());
//            if(user != null){
//                record.setStore(user.getNickname());
//            }
//        }
        Page<Goods> page = goodsService.findPage(new Page<>(pageNum, pageSize), name);
        return Result.success(page);

        }

    @GetMapping("/getUserDhsp")
    public Result getUserDhsp(@RequestParam Integer pageNum, @RequestParam Integer pageSize,@RequestParam Integer userId) {
        Page<duihuanjiluVo> page = goodsService.getUserDhsp(new Page<>(pageNum, pageSize), userId);
        return Result.success(page);

    }



}

